<template>
	<view>
		<view class="login">
			<view class="">
				<view class="" v-if="User.username">
					<image class="login_img"  :src="User.avator_img?User.avator_img:'../../static/images/tabBar/ecmoban_logo.png'" mode=""></image>
					<view class="login_h1">你好,&nbsp;&nbsp;{{User.username}}</view>
				</view>
				<view class="" v-else>
					<image class="login_img"  src="../../static/images/tabBar/ecmoban_logo.png" mode=""></image>
					<view class="login_h1" >{{userName}}</view>
				</view>
			</view>
			<view class="mt-3 border-bottom pb-2 input_height" >
				<view class="font-md pb-2">登录名称</view>
				<view class="d-flex j-sb">
					<input type="text" class="flex-1" placeholder="请输入登录名称" placeholder-class="font-md" v-model="userInfo.username" @input="numberInput" />
					<image v-if="userInfo.username" src="../../static/images/tabBar/icon_input_content_clear.png" mode="widthFix" class="ps_img mt-1" @click="numberClose"></image>
				</view>
			</view>
			<view class="mt-3 border-bottom pb-2 input_height">
				<view class="font-md pb-2">登录密码</view>
				<view class="d-flex j-sb">
					<input type="text" class="flex-1" :password="showPassword" placeholder="请输入密码" placeholder-class="font-md" v-model="userInfo.password" @input="passInput" />
					<image :src="[!showPassword?'../../static/images/tabBar/icon_password_hide.png':'../../static/images/tabBar/icon_password_show.png']" mode="widthFix" class="ps_img mt-1" @click="showIcon"></image>
				</view>
			</view>
			<button class="" type="warn" class="mt-4" @click="submit" :disabled="disabled">登录</button>
		</view>
		<view class="position-absolute w-100  bottom-0 text-center text-light-muted mb-5 pb-5" @click="check">查看更多</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex';
	export default {
		data() {
			return {
				userInfo:{
					username:'',
					password:''
				},
				disabled: true,
				userName:"欢迎使用世联掌柜",
				showPassword: true,
				User:{},
			}
		},
		methods: {
			showToast() {
				this.$refs.uToast.show({
					title: '登录成功',
					type: 'success',
				})
			},
			showToast_fill() {
				this.$refs.uToast.show({
					title: '你输入的账号信息不正确。',
					type: 'default',
				})
			},
			
			...mapMutations(['login']),
			...mapMutations(['webUrl']),
			// 显示 隐藏小图标
			showIcon(){
				 this.showPassword = !this.showPassword;
			},
			// 监听一下 账号输入框
			numberInput(e){
				this.userInfo.username=e.target.value
			},
			// 去掉输入框内容 
			numberClose(){
				this.userInfo.username=""
				if(uni.getStorageSync("toggle_default")){
					uni.removeStorageSync('users');
					console.log(111)
					uni.removeStorageSync('toggle_default');
					this.User={}
				}else if(uni.getStorageSync("toggle_demo")){
					uni.removeStorageSync('users_demo');
					console.log(222)
					uni.removeStorageSync('toggle_demo');
					this.User={}
				}else{
					uni.removeStorageSync('users');
					console.log(111)
					uni.removeStorageSync('toggle_default');
					this.User={}
				}	
			},
			// 监听一下 密码输入框
			passInput(e){
				this.userInfo.password=e.target.value
				if(this.userInfo.password!=""){
					 this.disabled = false;
				}else{
					 this.disabled = true;
				}
			},
			// 登录
			submit(){
				if(uni.getStorageSync("toggle_default")=="默认"){
					this.webUrl(this.localConfig.web_Url)
				}else if(uni.getStorageSync("toggle_demo")=="世联DEMO"){
					this.webUrl(this.localConfig.demo_Url)
				}else{
					this.webUrl(this.localConfig.web_Url)
				}
				
				this.$H.post("admin/user/signin",this.userInfo).then((res,rej)=>{
					console.log(res)
					if(res.status.succeed==1){
						uni.setStorageSync("session",res.data.session)
						if(res.data.session.sid){
							uni.setStorageSync("token",res.data.session.sid)
						}else{
							uni.setStorageSync("token",res.data.session.sid)
						}
            console.log(uni.getStorageSync('user_id'));
            uni.setStorageSync("user_id",res.data.userinfo.id)
            uni.$timlogin()
						this.login(res.data.userinfo)
						uni.switchTab({
							url:"/pages/home/home"
						})
						this.showToast()
						const client = uni.getSystemInfoSync();
						uni.setStorageSync("client",JSON.stringify(client))
					}else if(res.status.succeed==0){
						this.showToast_fill()
					}
				})
			},
			// 查看更多
			check(){
				uni.navigateTo({
					url:"/pages/login-set/login-set"
				})
			}
		},
		onShow() {
			uni.hideLoading();
			if(uni.getStorageSync("toggle_default")){
				this.User=uni.getStorageSync("users") || {}
				this.webUrl(this.localConfig.web_Url)
				this.userInfo.username=this.User.username
			}else if(uni.getStorageSync("toggle_demo")){
				this.User=uni.getStorageSync("users_demo") || {}
				this.webUrl(this.localConfig.demo_Url)
				this.userInfo.username=this.User.username
				
			}else{
				this.User=uni.getStorageSync("users") || {}
				this.webUrl(this.localConfig.web_Url)
				this.userInfo.username=this.User.username
			}
		},
	}
</script>

<style>
	page{
		background-color: #fff;
	}
	.login{
		margin: 100upx;
	}
	.login_h1{
		font-size: 50upx;
		padding: 10upx 0;
		font-weight:bold
	}
	.login /deep/ button{
		border-radius: 20upx;
	}
	.input_height{
		height: 130upx;
	}
</style>
